﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DTO;
using System.Data;

namespace DAO
{
    public class TaiKhoanDAO
    {
        DataProvider provider;

        public TaiKhoanDAO()
        {
            provider = new DataProvider();
        }

        public TaiKhoanDTO KiemTraDangNhap(TaiKhoanDTO taiKhoan)
        {
            string sqlStatement = "SELECT * FROM TaiKhoan WHERE TenTaiKhoan='" + taiKhoan.TenTaiKhoan
                + "' AND MatKhau='" + taiKhoan.MatKhau + "' AND DaXoa=0";

            DataTable dt = provider.ExecuteQuery(sqlStatement);
            if (dt != null)
            {
                if (dt.Rows.Count == 1)
                {
                    TaiKhoanDTO tk = new TaiKhoanDTO();
                    tk.MaTaiKhoan = Int32.Parse(dt.Rows[0]["MaTaiKhoan"].ToString());
                    tk.TenTaiKhoan = dt.Rows[0]["TenTaiKhoan"].ToString();
                    tk.MatKhau = dt.Rows[0]["MatKhau"].ToString();
                    tk.HoTen = dt.Rows[0]["HoTen"].ToString();
                    tk.CMND = dt.Rows[0]["CMND"].ToString();
                    tk.DienThoai = dt.Rows[0]["DienThoai"].ToString();
                    tk.DiaChi = dt.Rows[0]["DiaChi"].ToString();
                    tk.Email = dt.Rows[0]["Email"].ToString();
                    tk.MaCauHoi = Int32.Parse(dt.Rows[0]["MaCauHoi"].ToString());
                    tk.CauTraLoi = dt.Rows[0]["CauTraLoi"].ToString();
                    tk.TenCongTy = dt.Rows[0]["TenCongTy"].ToString();
                    tk.SoLanThamGiaDauGia = Int32.Parse(dt.Rows[0]["SoLanThamGiaDauGia"].ToString());
                    tk.SoLanGiaoDichVoiCongTy = Int32.Parse(dt.Rows[0]["SoLanGiaoDichVoiCongTy"].ToString());
                    tk.PhanQuyen = Int32.Parse(dt.Rows[0]["PhanQuyen"].ToString());
                    return tk;
                }
                return null;
            }
            return null;
        }

        public bool KiemTraTenTaiKhoan(TaiKhoanDTO taiKhoan)
        {
            string sqlStatement = "SELECT * FROM TaiKhoan WHERE TenTaiKhoan='" + taiKhoan.TenTaiKhoan + "'";
            DataTable dt = provider.ExecuteQuery(sqlStatement);
            if (dt.Rows.Count == 0)
                return true;
            return false;
        }

        public TaiKhoanDTO LayTaiKhoanTheoMaTaiKhoan(TaiKhoanDTO taiKhoan)
        {
            TaiKhoanDTO tk = null;

            string sqlStatement = "SELECT * FROM TaiKhoan WHERE MaTaiKhoan=" + taiKhoan.MaTaiKhoan;
            DataTable dt = provider.ExecuteQuery(sqlStatement);
            if (dt != null)
            {
                tk = new TaiKhoanDTO();
                tk.MaTaiKhoan = Int32.Parse(dt.Rows[0]["MaTaiKhoan"].ToString());
                tk.TenTaiKhoan = dt.Rows[0]["TenTaiKhoan"].ToString();
                tk.MatKhau = dt.Rows[0]["MatKhau"].ToString();
                tk.HoTen = dt.Rows[0]["HoTen"].ToString();
                tk.CMND = dt.Rows[0]["CMND"].ToString();
                tk.DienThoai = dt.Rows[0]["DienThoai"].ToString();
                tk.DiaChi = dt.Rows[0]["DiaChi"].ToString();
                tk.Email = dt.Rows[0]["Email"].ToString();
                tk.MaCauHoi = Int32.Parse(dt.Rows[0]["MaCauHoi"].ToString());
                tk.CauTraLoi = dt.Rows[0]["CauTraLoi"].ToString();
                tk.TenCongTy = dt.Rows[0]["TenCongTy"].ToString();
                tk.SoLanThamGiaDauGia = Int32.Parse(dt.Rows[0]["SoLanThamGiaDauGia"].ToString());
                tk.SoLanGiaoDichVoiCongTy = Int32.Parse(dt.Rows[0]["SoLanGiaoDichVoiCongTy"].ToString());
                tk.PhanQuyen = Int32.Parse(dt.Rows[0]["PhanQuyen"].ToString());
                tk.DaXoa = bool.Parse(dt.Rows[0]["DaXoa"].ToString());
            }
            return tk;
        }

        public int ThemTaiKhoan(TaiKhoanDTO taiKhoan)
        {
            //if (KiemTraTenTaiKhoan(taiKhoan)==false)
            //    return 2;

            string sqlStatement = "INSERT INTO TaiKhoan(TenTaiKhoan, MatKhau, HoTen, CMND, DiaChi, DienThoai, Email, MaCauHoi, CauTraLoi, TenCongTy, SoLanThamGiaDauGia, SoLanGiaoDichVoiCongTy, PhanQuyen, DaXoa)"
                + " VALUES ('" + taiKhoan.TenTaiKhoan + "', '" + taiKhoan.MatKhau + "', '" + taiKhoan.HoTen
                + "', '" + taiKhoan.CMND + "', '" + taiKhoan.DienThoai + "', '" + taiKhoan.DiaChi + "', '" + taiKhoan.Email
                + "', " + taiKhoan.MaCauHoi + ", '" + taiKhoan.CauTraLoi + "', '" + taiKhoan.TenCongTy 
                + "', " + taiKhoan.SoLanThamGiaDauGia + ", " + taiKhoan.SoLanGiaoDichVoiCongTy + ", " + taiKhoan.PhanQuyen + ",0)";

            return provider.ExecuteNonQuery(sqlStatement);
        }

        public int CapNhatTaiKhoan(TaiKhoanDTO taiKhoan)
        {
            string sqlStatement = "UPDATE TaiKhoan SET MatKhau='" + taiKhoan.MatKhau
                + "', HoTen='" + taiKhoan.HoTen + "', CMND='" + taiKhoan.CMND
                + "', DienThoai='" + taiKhoan.DienThoai + "', DiaChi='" + taiKhoan.DiaChi
                + "', Email='" + taiKhoan.Email + "', MaCauHoi=" + taiKhoan.MaCauHoi + ", CauTraLoi='" + taiKhoan.CauTraLoi 
                + "', TenCongTy='" + taiKhoan.TenCongTy + "', SoLanThamGiaDauGia=" + taiKhoan.SoLanThamGiaDauGia 
                + ", SoLanGiaoDichVoiCongTy=" + taiKhoan.SoLanGiaoDichVoiCongTy
                + ", PhanQuyen=" + taiKhoan.PhanQuyen + " WHERE MaTaiKhoan=" + taiKhoan.MaTaiKhoan;

            return provider.ExecuteNonQuery(sqlStatement);
        }

        public int XoaTaiKhoan(TaiKhoanDTO taiKhoan)
        {
            string sqlStatement = "UPDATE TaiKhoan SET DaXoa=1 WHERE MaTaiKhoan=" + taiKhoan.MaTaiKhoan;

            return provider.ExecuteNonQuery(sqlStatement);
        }
    }
}
